package cn.wolfcode.wms.web.controller;



import cn.wolfcode.wms.query.Dictionaty;
import cn.wolfcode.wms.query.OrderChartQueryObject;
import cn.wolfcode.wms.query.QueryObject;
import cn.wolfcode.wms.query.SaleChartQueryObject;
import cn.wolfcode.wms.service.IBrandService;
import cn.wolfcode.wms.service.IChartService;
import cn.wolfcode.wms.service.IClientService;
import cn.wolfcode.wms.service.ISupplierService;
import cn.wolfcode.wms.util.JSONResult;
import cn.wolfcode.wms.util.NeedPermission;
import cn.wolfcode.wms.util.PageResult;
import com.alibaba.druid.support.json.JSONUtils;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.sun.xml.internal.ws.api.model.MEP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("chart")
public class ChartController {
    @Autowired
    private IChartService chartService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private IClientService clientService;
    @Autowired
    private ISupplierService supplierService;

    @RequestMapping("sale")
    public String list(@ModelAttribute("qo") SaleChartQueryObject qo, Model model){
        List<Map<String, Object>> list = chartService.querySaleChart(qo);
        model.addAttribute("result",list);
        model.addAttribute("brands",brandService.selectList());
        model.addAttribute("clients",clientService.selectList());

        return "chart/list";
    }

    @RequestMapping("saleByBar")
    public String saleByBar(@ModelAttribute("qo") SaleChartQueryObject qo, Model model){

        //定义两个集合,分别用来存储key和value
        List<Object> x = new ArrayList<>();
        List<Object> y = new ArrayList<>();
        List<Map<String, Object>> maps = chartService.querySaleChart(qo);
        for (Map<String, Object> map : maps) {
            x.add(map.get("groupType"));
            y.add(map.get("amount"));
        }
        //把集合转换成JSON对象
        model.addAttribute("groupType", Dictionaty.SALE_MAP.get(qo.getGroupType()));
        model.addAttribute("x", JSONUtils.toJSONString(x));
        model.addAttribute("y", JSONUtils.toJSONString(y));
        return "chart/saleByBar";
    }
    @RequestMapping("saleByPie")
    public String saleByPie(@ModelAttribute("qo") SaleChartQueryObject qo, Model model){

        //定义两个集合,分别用来存储key和value
        List<Object> x = new ArrayList<>();//格式:{amdin,总值}
        List<Object> y = new ArrayList<>();
        List<Map<String, Object>> maps = chartService.querySaleChart(qo);
        for (Map<String, Object> map : maps) {
            x.add(map.get("groupType"));
            Map<String,Object> obj = new HashMap<>();
            obj.put("name",map.get("groupType"));
            obj.put("value",map.get("amount"));
            y.add(obj);
        }
        //把集合转换成JSON对象
        model.addAttribute("groupType", Dictionaty.SALE_MAP.get(qo.getGroupType()));
        model.addAttribute("x", JSONUtils.toJSONString(x));
        model.addAttribute("y", JSONUtils.toJSONString(y));
        return "chart/saleByPie";
    }

    @RequestMapping("order")
    public String order(@ModelAttribute("qo") OrderChartQueryObject qo, Model model){
        List<Map<String, Object>> list = chartService.queryOrderChart(qo);
        model.addAttribute("result",list);
        model.addAttribute("brands",brandService.selectList());
        model.addAttribute("suppliers",supplierService.selectList());

        return "chart/order";
    }

}
